<template>
  <el-form style="width:40%;" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="编号" prop="id">
      <el-input v-model="ruleForm.id" :disabled="true"></el-input>
    </el-form-item>

    <el-form-item label="用户id" prop="user_id">
      <el-input v-model="ruleForm.user_id" :disabled="true"></el-input>
    </el-form-item>

    <el-form-item label="猫咪id" prop="cat_id">
      <el-input v-model="ruleForm.cat_id" :disabled="true"></el-input>
    </el-form-item>

    <el-form-item label="数量/只" prop="cat_count">
      <el-input v-model="ruleForm.cat_count"></el-input>
    </el-form-item>

    <el-form-item label="价格/元" prop="cat_price">
      <el-input v-model="ruleForm.cat_price"></el-input>
    </el-form-item>

    <el-form-item label="支持优惠券" prop="cat_is_coupon">
      <el-select v-model="ruleForm.cat_is_coupon" placeholder="请选择">
        <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"
                   :disabled="item.disabled">
        </el-option>
      </el-select>
    </el-form-item>

    <el-form-item>
      <el-button type="info" @click="cancel()">取消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">修改</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
  const axios = require('axios')
  export default {
    data() {
      return {
        ruleForm: {
          id: '',
          user_id: '',
          cat_id: '',
          cat_count: '',
          cat_price: '',
          cat_is_coupon: ''
        },
        rules: {
          cat_count: [{required: true, message: '数量不能为空', trigger: 'blur'}],
          cat_price: [{required: true, message: '价格不能为空', trigger: 'blur'}],
          cat_is_coupon: [{required: true, message: '优惠券不能为空', trigger: 'blur'}]
        },
        options: [{
          value: '支持',
          label: '支持'
        }, {
          value: '不支持',
          label: '不支持'
        }, {
          value: '已失效',
          label: '已失效',
          disabled: true
        }]
      };
    },
    methods: {
      submitForm(formName) {  // 修改按钮
        const _this = this
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios.put('http://localhost:9000/cars/update', this.ruleForm).then(function (resp) {
              console.log(resp)
              if (resp.data == 'success') {
                _this.$alert('购物车id <' + _this.ruleForm.id + '>已修改', '消息', {
                  confirmButtonText: '确定',
                  callback: action => {
                    _this.$router.push('/user/cars')
                  }
                });
              }
            });
          } else {
            return false;
          }
        })
      },
      cancel() {
        this.$router.go(-1)
        this.$message({
          type: 'warning',
          message: '取消修改'
        })
      }
    },
    created() {
      console.log('更新id：' + this.$route.query.id)
      const _this = this
      this.$axios.get('http://localhost:9000/cars/findById/' + this.$route.query.id).then(function (resp) {
        _this.ruleForm = resp.data
      })
    }
  }
</script>
